const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        name: "", // 姓名
        phone: "", // 手机号
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        this.initData();
    },
    /**
     * @function 初始化页面数据
     */
    initData() {
        const { zs_name, tel } = wx.getStorageSync("userInfo");
        this.setData({
            name: zs_name,
            phone: tel,
        });
    },
    sumbit() {
        const name = this.data.name;
        const phone = this.data.phone;
        const reg =
            /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
        if (!name || !phone) {
            wx.showToast({
                title: "请填写完整！",
                icon: "error",
            });
            return;
        }
        if (!reg.test(phone)) {
            wx.showToast({
                title: "手机号格式不对",
                icon: "error",
            });
            return;
        }

        app.util.request({
            url: "entry/wxapp/Distribution",
            data: {
                // user_id: app.user_info.user_id, // app全局对象获取用户id
                user_id: wx.getStorageSync("userInfo").id, // app全局对象获取用户id
                user_name: this.data.name,
                user_tel: this.data.phone,
            },
            success: function (t) {
                wx.showToast({
                    title: "已提交申请",
                });
                // 刷新小程序的缓存1
                app.util.request({
                    url: "entry/wxapp/GetSystem",
                    cachetime: "0",
                    success: function (t) {
                        wx.setStorageSync("platform", t.data);
                    },
                });
                // 刷新小程序的缓存2
                app.getUserInfo();
                setTimeout(() => {
                    wx.navigateBack();
                }, 2000);
            },
        });
    },
    goback() {
        wx.navigateBack();
    },
});
